package com.burgeon.backend.SHOPBACKEND.rest.openCardTask;

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.openCardTask.EMPListData;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;


public class FindEmpListView extends RESTBase {


    private String ip;
    private String account;
    private String password;

    public FindEmpListView(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }


    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<EMPListData> getData(String id) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(id), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");

        ArrayList<EMPListData> empListData = new ArrayList<EMPListData>();

        for (int i = 0; i < jsonArray.length(); i++) {


            EMPListData emp = new EMPListData();
            emp.setId(jsonArray.getJSONArray(i).getString(0));
            emp.setName(jsonArray.getJSONArray(i).getString(1));
            emp.setTarget(jsonArray.getJSONArray(i).getString(2));
            emp.setFinish(jsonArray.getJSONArray(i).getString(3));
            emp.setPercentage(jsonArray.getJSONArray(i).getString(4));
            emp.setEmpId(jsonArray.getJSONArray(i).getString(5));
            empListData.add(emp);
        }
        return empListData;
    }

    private MultiValueMap Data(String id) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("'HR_EMPLOYEE_ID;NAME'");
        columns.add("TOT_AMT_MARK");
        columns.add("TOT_OPENVIP");
        columns.add("COM_RATE");
        columns.add("HR_EMPLOYEE_ID");


        params.put("columns", columns);
        params.put("table", "C_STOREODMARKITEM");//表名

        //设置查询规则（从第几条查询，每页显示多少条数）

        //设置排序规则
        HashMap orderby = new HashMap();
        orderby.put("column", "TOT_OPENVIP");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        HashMap paramss = new HashMap();
        paramss.put("column", "C_STOREODMARK_ID");
        paramss.put("condition", "'=" + id + "'");
        params.put("params", paramss);


        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        FindEmpListView querryYCRetailout = new FindEmpListView("39.104.73.78:5321", "nea@burgeon.com.cn", "burgeon5321");
        try {
            System.err.println(querryYCRetailout.getData("7"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
